﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using tantrinh.Models;

namespace tantrinh.Controllers
{ 
    public class SanPhamController : Controller
    {
        private TanTrinhContext db = new TanTrinhContext();

        //
        // GET: /SanPham/

        public ViewResult Index()
        {
            var sanpham = db.SanPham.Include(s => s.ChiTietDanhMuc).Include(s => s.NhanHieu);
            return View(sanpham.ToList());
        }

        //
        // GET: /SanPham/Details/5

        public ViewResult Details(int id)
        {
            SanPham sanpham = db.SanPham.Find(id);
            return View(sanpham);
        }

        public ViewResult ViewDetails(int id)
        {
            SanPham sanpham = db.SanPham.Find(id);
            return View(sanpham);
        }
        //
        // GET: /SanPham/Create

        public ActionResult Create()
        {
            ViewBag.ChiTietDanhMucID = new SelectList(db.ChiTietDanhMuc, "ChiTietDanhMucID", "TenChiTietDanhMuc");
            ViewBag.NhanHieuID = new SelectList(db.NhanHieu, "NhanHieuID", "TenNhanHieu");
            return View();
        } 

        //
        // POST: /SanPham/Create

        [HttpPost]
        public ActionResult Create(SanPham sanpham)
        {
            if (ModelState.IsValid)
            {
                db.SanPham.Add(sanpham);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.ChiTietDanhMucID = new SelectList(db.ChiTietDanhMuc, "ChiTietDanhMucID", "TenChiTietDanhMuc", sanpham.ChiTietDanhMucID);
            ViewBag.NhanHieuID = new SelectList(db.NhanHieu, "NhanHieuID", "TenNhanHieu", sanpham.NhanHieuID);
            return View(sanpham);
        }
        
        //
        // GET: /SanPham/Edit/5
 
        public ActionResult Edit(int id)
        {
            SanPham sanpham = db.SanPham.Find(id);
            ViewBag.ChiTietDanhMucID = new SelectList(db.ChiTietDanhMuc, "ChiTietDanhMucID", "TenChiTietDanhMuc", sanpham.ChiTietDanhMucID);
            ViewBag.NhanHieuID = new SelectList(db.NhanHieu, "NhanHieuID", "TenNhanHieu", sanpham.NhanHieuID);
            return View(sanpham);
        }

        //
        // POST: /SanPham/Edit/5

        [HttpPost]
        public ActionResult Edit(SanPham sanpham)
        {
            if (ModelState.IsValid)
            {
                db.Entry(sanpham).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.ChiTietDanhMucID = new SelectList(db.ChiTietDanhMuc, "ChiTietDanhMucID", "TenChiTietDanhMuc", sanpham.ChiTietDanhMucID);
            ViewBag.NhanHieuID = new SelectList(db.NhanHieu, "NhanHieuID", "TenNhanHieu", sanpham.NhanHieuID);
            return View(sanpham);
        }

        //
        // GET: /SanPham/Delete/5
 
        public ActionResult Delete(int id)
        {
            SanPham sanpham = db.SanPham.Find(id);
            return View(sanpham);
        }

        //
        // POST: /SanPham/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            SanPham sanpham = db.SanPham.Find(id);
            db.SanPham.Remove(sanpham);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}